﻿using AuthCenter.CSM.Common;
using AuthCenter.CSM.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;

namespace AuthCenter.CSM.plugins
{
    internal class LogPlug : AuthCenterPluginBase
    {
        protected override string ListenUri { get; } = HttpCommon.SiteName +"/log";
        protected override async Task OnRequest(AuthCenterSession session)
        {
            if (session.IsPost(HttpCommon.SiteName+"/log"))
            {
                var output = new AuthCenterOutput();
                output.Success = true;

                if (session.IsAuthenticated)
                {
                    var data = await session.GetData<AuthCenterInput>();
                    switch (data.Method)
                    {
                        case "list-log": // 用户信息
                            var page = data.GetNumber("pageno");
                            var pagesize = data.GetNumber("pagesize");
                            var total = session.db_log.GetLogCount(session.UserID);
                            output.Params["total"] = total.ToString();
                            if (page < 1)
                            {
                                output.Success = false;
                            }
                            
                            if (output.Success)
                            {
                                var start=(page - 1) * pagesize;
                                var logs = session.db_log.GetLogs(session.UserID, start, pagesize);
                                output.Params["logs"] = JsonConvert.SerializeObject(logs);
                            }
                            break;
                        case "list-log-err":
                            var e_page = data.GetNumber("pageno");
                            var e_pagesize = data.GetNumber("pagesize");
                            var e_total = session.db_log.GetLogCount(session.UserID);
                            output.Params["total"] = e_total.ToString();
                            if (e_page < 1)
                            {
                                output.Success = false;
                            }

                            if (output.Success)
                            {
                                var start = (e_page - 1) * e_pagesize;
                                var logs = session.db_log.GetLogs(session.UserID, start, e_pagesize);
                                output.Params["logs"] = JsonConvert.SerializeObject(logs);
                            }
                            break;
                        default:
                            output.Success = false;
                            break;
                    }
                }
                else
                {
                    output.Success = false;
                }

                await session.Send(200, output)
                        .AnswerAsync();//直接回应
            }

            //无法处理，调用下一个插件
            await session.InvokeNext();
        }
    }
}
